# 全屏显示壁球小游戏
"""
全屏模式：
1、pygame.display.Info()
    利用这个函数返回的Videoinfo对象，获得当前屏幕的分辨率属性；
    这样就能让pygame知道你的屏幕全屏有多大尺寸；
2、pygame.display.set_mode()
    传入flags=pygame.FULLSCREEN
    将窗口设置成全屏模式；
"""
import sys
import os
import pygame

pygame.init()

# 全屏显示需要注意两个地方：
# 1、利用Info()函数获取当前显示器的分辨率
vInfo = pygame.display.Info()
size = width, height = vInfo.current_w, vInfo.current_h

BASE_DIR = os.path.realpath(os.path.dirname(sys.argv[0]))
BALL_DIR = os.path.join(BASE_DIR, 'ball.gif')
MUSIC_DIR = os.path.join(BASE_DIR, 'music.mp3')
speed = [1, 1]
WHITE = (255, 255, 255)
fps = 100

# 2、传入flags参数在set_mode()函数中设置全屏模式
screen = pygame.display.set_mode(size, flags=pygame.FULLSCREEN)

pygame.display.set_caption('ball')
clock = pygame.time.Clock()
ball = pygame.image.load(BALL_DIR)
ball_rect = ball.get_rect()

# 设置BGM
pygame.mixer.music.load(MUSIC_DIR)
pygame.mixer.music.play()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                speed[0] -= 1
            elif event.key == pygame.K_RIGHT:
                speed[0] += 1
            elif event.key == pygame.K_UP:
                speed[1] -= 1
            elif event.key == pygame.K_DOWN:
                speed[1] += 1
            elif event.key == pygame.K_SPACE:
                speed[1] = 0
                speed[0] = 0
            elif event.key == pygame.K_ESCAPE:
                sys.exit()

    ball_rect = ball_rect.move(speed[0], speed[1])
    if ball_rect.left < 0 or ball_rect.right > width:
        speed[0] = -speed[0]
    if ball_rect.top < 0 or ball_rect.bottom > height:
        speed[1] = -speed[1]

    screen.fill(WHITE)
    screen.blit(ball, ball_rect)
    clock.tick(fps)
    pygame.display.update()
